package com.source.lightfowl.person.dao;

import com.source.lightfowl.person.entity.Person;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Transient;
import java.math.BigInteger;
import java.util.List;

@Transactional
public interface PersonDao extends JpaRepository<Person, String> {

    /**
     * 整改表为sys_user
     */
    @Modifying
    @Query(value = "INSERT INTO `sys_user` " +
            "( `id`, `loginName`, `password`, `email`, `createDate`)" +
            " VALUES (?1, ?2, ENCODE(?3, ?4), ?5, ?6)",nativeQuery = true)
    int register(String id, String username, byte[] password, String salt, String email, String date);

    @Modifying
    @Query(value = "INSERT INTO `sys_user` " +
            "( `id`, `loginName`, `password`, `email`, `createDate`,`wechatCode`,`wechatCodeDecode`)" +
            " VALUES (?1, ?2, ENCODE(?3, ?4), ?5, ?6, ?7, ?8)",nativeQuery = true)
    int register2(String id, String username, byte[] password, String salt, String email, String date, String codePath, String decode);

    @Modifying
    @Query(value = "INSERT INTO `sys_user` " +
            "(`password`, `email`, `updateDate`)" +
            " VALUES (ENCODE(?1, ?2), ?3, ?4)",nativeQuery = true)
    int updatePassword(byte[] bytes, String salt, String email, String date);

    @Query(value = "SELECT * FROM `sys_user` WHERE (`loginName` = ?1 AND `password` = ENCODE(?2, ?3))" +
            " OR (`email` = ?1 AND `password` = ENCODE(?2, ?3))"
            ,nativeQuery = true)
    Person login(String username, byte[] password, String salt);

    @Query(value = "SELECT * FROM `sys_user` WHERE `loginName` = ?1",nativeQuery = true)
    Person checkUsername(String username);

    @Query(value = "SELECT * FROM `sys_user` WHERE `email` = ?1",nativeQuery = true)
    List<Person> checkEmail(String email);

    @Query(value = "SELECT `person_id`,`loginName`,`password`, `email`,`createDate` " +
            "FROM `sys_user` WHERE `id` = ?1",nativeQuery = true)
    Person findPersonById(String fid);


    @Query(value = "SELECT * FROM `sys_user` WHERE `id` = ?1",nativeQuery = true)
    Person findVO(String id);
    /*@Query(value = "SELECT * FROM `sys_user` WHERE `id` = ?1",nativeQuery = true)
    Person findVO(BigInteger id);*/

    @Query(value = "SELECT * FROM `sys_user` WHERE `loginName` = ?1",nativeQuery = true)
    Person findByUserName(String username);

    Person findByName(String name);

    @Modifying
    @Query(value = "update `sys_user` set idCard=?1 WHERE id=?2",nativeQuery = true)
    void updateIdCardById(String idCardNumber, String id);

    @Modifying
    @Query(value = "update `sys_user` set idCard=null WHERE id=?1",nativeQuery = true)
    void updateIdCardNull(String userId);

    @Modifying
    @Query(value = "update `sys_user` set role=null WHERE role=?1",nativeQuery = true)
    void deleteRoleNull(String roleId);
/*
    @Modifying
    @Query(value = "INSERT INTO `person` " +
            "( `username`, `password`, `email`, `register_time`)" +
            " VALUES (?1, ENCODE(?2, ?3), ?4, ?5)",nativeQuery = true)
    int register(String username, byte[] password, String salt, String email, String date);

    @Modifying
    @Query(value = "INSERT INTO `person` " +
            "(`password`, `email`, `register_time`)" +
            " VALUES (ENCODE(?1, ?2), ?3, ?4)",nativeQuery = true)
    int updatePassword(byte[] bytes, String salt, String email, String date);

    @Query(value = "SELECT * FROM `person` WHERE (`username` = ?1 AND `password` = ENCODE(?2, ?3))" +
            " OR (`email` = ?1 AND `password` = ENCODE(?2, ?3))"
            ,nativeQuery = true)
    Person login(String username, String password, String salt);

    @Query(value = "SELECT * FROM `person` WHERE `username` = ?1",nativeQuery = true)
    Person checkUsername(String username);

    @Query(value = "SELECT * FROM `person` WHERE `email` = ?1",nativeQuery = true)
    Person checkEmail(String email);


//    @Modifying
//    @Query(value = "UPDATE introduction SET " +
//            "`name`=?1, `sex`=?2, `relationship_status`=?3," +
//            "`hobby`=?4 WHERE `person_id`=?5",nativeQuery = true)
//    int updateIntroduction(String name, String sex,
//                           String relationshipStatus, String hobby, String personId);
//
//    @Modifying
//    @Query(value = "INSERT INTO `introduction` " +
//            "(`person_id`) VALUES (?1)",nativeQuery = true)
//    int addIntroduction(String personId);
//


 */
}
